package dao;

// Generated 04-Apr-2011 11:04:34 by Hibernate Tools 3.4.0.CR1

import java.util.List;

import hibernate.HibernateDAO;
import beans.TbBoleto;
import javax.ejb.Stateless;
import org.hibernate.criterion.Restrictions;

@Stateless
public class TbBoletoDAO extends HibernateDAO<TbBoleto, Long> {
	public TbBoleto findByCodMaq(int cod) {
		return this.findByCriteria(Restrictions.eq("tbMaquina.maquinaCod", cod), Restrictions.eq("boletoStatus", 'A'));
	}
	
	public List<TbBoleto> ListarBoletosByCod(int cod) {
		return this.listByQuery("from TbBoleto b where b.tbOp.tbProduto.produtoCod = " + cod + 
				                " and b.boletoStatus <> 'C' order by (CASE WHEN b.boletoStatus = 'A' THEN 0 " +
				                                                          "WHEN b.boletoStatus = 'F' THEN 1 " +
				                                                          "WHEN b.boletoStatus = 'I' THEN 2 END), b.tbOp.tbProduto.produtoCod");
	}
	
	
	public List<TbBoleto> ListarBoletos() {
		return this.listByQuery("from TbBoleto b " +
				                "order by (CASE WHEN b.boletoStatus = 'A' THEN 0 " +
				                               "WHEN b.boletoStatus = 'F' THEN 1 " +
				                               "WHEN b.boletoStatus = 'I' THEN 2 " +
		                                       "WHEN b.boletoStatus = 'C' THEN 3 END), b.boletoCod");
	}


	public String ListarBoletosCliente(int cod) {
		return this.session.createQuery("Select sum(b.boletoMeta - b.boletoQtproduzida)from TbBoleto b where b.tbOp.tbCliente.clienteCod = " + Integer.toString(cod) +
				                        " and b.boletoStatus != 'C' order by b.boletoCod").list().get(0).toString();
	}
	
	public List<TbBoleto> ListarBoletosStatus(String status) {
	return this.session.createQuery("from TbBoleto b where b.boletoStatus = '" + status + "' order by b.boletoCod").list();
}
	
	
	

	public List<TbBoleto> ListarPorMaquina(String cod) {
		return listByQuery("from TbBoleto b where b.tbMaquina.maquinaCod = " + cod + " and b.boletoStatus <> 'A' order by b.boletoPrioridade, b.tbOp.opCod, b.boletoCod");
	}
}
